package com.easyfun.healthmagicbox.clocksetting;

import java.net.MalformedURLException;
import java.net.URL;

import org.json.JSONException;
import org.json.JSONObject;

import com.easyfun.healthmagicbox.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ClockSettingActivity extends Activity {


	private static final String ADD_URL = "http://192.168.10.1/sys";

	protected static final String CLOCKAP_SSID = "WM";

	private Button mBtn1;
	private EditText wifissid = null;
	private EditText wifiphase = null;

	private WifiAdmin mWifiAdmin;

	private Context mContext = null;
	private final String LOG_TAG = getClass().getSimpleName();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mContext = this;

		setContentView(R.layout.clocksetting);

		wifissid = (EditText) findViewById(R.id.wifissid);
		wifiphase = (EditText) findViewById(R.id.wifiphase);
		mBtn1 = (Button) findViewById(R.id.butWificonnect);

		mBtn1.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				mWifiAdmin = new WifiAdmin(mContext) {

					@Override
					public void myUnregisterReceiver(BroadcastReceiver receiver) {
						// TODO Auto-generated method stub
						ClockSettingActivity.this.unregisterReceiver(receiver);
					}

					@Override
					public Intent myRegisterReceiver(
							BroadcastReceiver receiver, IntentFilter filter) {
						// TODO Auto-generated method stub
						ClockSettingActivity.this.registerReceiver(receiver,
								filter);
						return null;
					}

					
					@Override
					public void onNotifyWifiConnected() {
						// TODO Auto-generated method stub
						Log.v(LOG_TAG, "have connected success!");
						Log.v(LOG_TAG, "###############################");
						SendWifiSettingToClock();
					}

					@Override
					public void onNotifyWifiConnectFailed() {
						// TODO Auto-generated method stub
						Log.v(LOG_TAG, "have connected failed!");
						Log.v(LOG_TAG, "###############################");
					}
				};
				mWifiAdmin.openWifi();
				
				mWifiAdmin.addNetwork(CLOCKAP_SSID, "",
						WifiAdmin.TYPE_NO_PASSWD);
				SendWifiSettingToClock();
				
			}
		});
/*
		mBtn2.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				WifiApAdmin wifiAp = new WifiApAdmin(mContext);
				wifiAp.startWifiAp("\"HotSpot\"", "hhhhhh123");
			}
		});
*/
	}

	private void SendWifiSettingToClock() {
		JSONObject jsonObj = new JSONObject();
		URL url = null;
		try {
			jsonObj.put("app_name", "asdf");
			jsonObj.put("app_ip", "10.21.243.234");
			jsonObj.put("app_port", 8080);
			jsonObj.put("app_type", "001");
			jsonObj.put("app_area", "asd");
			url = new URL(ADD_URL);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return;
		}
		String result = PostJsonToClock.post(url, jsonObj);
		Log.i(LOG_TAG, "post result:"+result);
		
		//go back to home wifi
		mWifiAdmin.openWifi();
		String wifiSSID = wifissid.getText().toString();
		String wifiPhase = wifiphase.getText().toString();
		if ((wifiPhase != null) && (wifiPhase.length() > 0))
			mWifiAdmin.addNetwork(mWifiAdmin.createWifiInfo(wifiSSID,
					wifiPhase, WifiAdmin.TYPE_WPA));
		else
			mWifiAdmin.addNetwork(wifiSSID, "",
					WifiAdmin.TYPE_NO_PASSWD);
	}

	@Override
	public void onResume() {
		super.onResume();

		Log.d("Rssi", "Registered");
	}

	@Override
	public void onPause() {
		super.onPause();

		Log.d("Rssi", "Unregistered");
	}

}
